﻿namespace Yaaf.Wp7.AdvancedExplorer.ViewModels
{
    using System.ComponentModel;
    using System.Windows.Media.Imaging;

    using Yaaf.WP7.NativeAccess.Registry;

    public class RegistryEntryViewModel : INotifyPropertyChanged
    {
        private readonly RegistryValue regValue;
        
        private readonly RegistryKey subKey;

        private bool isSelected;

        private string elementName;

        public RegistryEntryViewModel(RegistryKey subKey)
        {
            this.subKey = subKey;
            var index = subKey.Name.LastIndexOf('\\');
            string simpleName;
            if (index < 0)
            {
                simpleName = subKey.Name;
            }
            else
            {
                simpleName = subKey.Name.Substring(index + 1);
            }
            this.elementName = simpleName;
        }
        
        public RegistryEntryViewModel(RegistryValue regValue)
        {
            this.regValue = regValue;
            var fullPath = regValue.FullPath;
            this.elementName = fullPath.Substring(fullPath.LastIndexOf('\\') + 1);
        }

        public BitmapImage Icon
        {
            get
            {
                if (subKey != null)
                {
                    return IconStore.IconFolder;
                }
                else
                {
                    return IconStore.IconFile;
                }
            }
        }

        public string ElementName
        {
            get
            {
                return this.elementName;
            }
            set
            {
                this.elementName = value;
            }
        }

        public bool IsSelected
        {
            get
            {
                return this.isSelected;
            }
            set
            {
                this.isSelected = value;
                this.NotifyPropertyChanged("IsSelected");
            }
        }

        public RegistryKey Element
        {
            get
            {
                return subKey;
            }
        }

        public RegistryValue Value
        {
            get
            {
                return regValue;
            }
        }

        public string FullPath
        {
            get
            {
                if (subKey != null)
                {
                    return subKey.Name;
                }
                else
                {
                    return regValue.FullPath;
                }
            }
        }

        public event PropertyChangedEventHandler PropertyChanged;

        public void NotifyPropertyChanged(string propertyName)
        {
            PropertyChangedEventHandler handler = this.PropertyChanged;
            if (handler != null)
            {
                handler(this, new PropertyChangedEventArgs(propertyName));
            }
        }
    }
}